/*
Copyright (C) 1996-1997 Id Software, Inc.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 3
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/
// client.h

#pragma once

typedef struct
{
	vec3_t	viewangles;

	// intended velocities
	float	forwardmove;
	float	sidemove;
	float	upmove;
} usercmd_t;

typedef struct
{
	int		length;
	char	map[MAX_STYLESTRING];
} lightstyle_t;

typedef struct
{
	char	name[MAX_SCOREBOARDNAME];
	float	entertime;
	int		frags;
	int		colors;			// two 4 bit fields
	byte	translations[VID_GRADES * 256];
} scoreboard_t;

typedef struct cshift_s
{
	float	destcolor[3];
	float	percent;		// 0-256

	float	initialpercent;
	double	inittime;
} cshift_t;

#define	CSHIFT_DAMAGE	0
#define	CSHIFT_BONUS	1
#define CSHIFT_QUAD		2
#define CSHIFT_SUIT		3
#define CSHIFT_RING		4
#define CSHIFT_PENT		5
#define CSHIFT_VCSHIFT	6

#define	NUM_CSHIFTS		7

#define	NAME_LENGTH	64


// client_state_t should hold all pieces of the client state
#define	SIGNONS		4			// signon messages to receive before connected

#define	MAX_DLIGHTS		32

struct dlight_t
{
public:
	float	origin[3];
	float	angles[3];

	// padded for cbuffer input
	float	transformed[4];

	// for FPS-independent decay
	float	baseradius;
	float	basetime;

	float	maxdist;
	float	radius;
	float	decay;				// drop this each second
	float	minlight;			// don't add when contributing less

	float	color[3];
	float	ambientscale;
	float	diffusescale;
	float	falloff;
	float	spotvec[3];

	float	die;				// stop lighting after this time
	int		key;
	int		flags;

	int		style;
	float	stylevalue;

	BOOL	Marked;
	BOOL	Shadow;
	BOOL	Inside;

	int		visframe;	// used for visibility testing of this light

	void Decay (void);
	void Draw (struct entity_t *ent);
	void DrawBrush (struct entity_t *ent, struct model_t *m, struct QSHADER *shader);
	void DrawMesh (struct entity_t *ent, struct aliashdr_t *hdr, struct QSHADER *shader);
	void MarkLights (struct model_t *m, struct mnode_t *node);
	BOOL Alive (void);
	void Initialize (int key, float radius, float die, float r, float g, float b);
};


#define	MAX_MAPSTRING	2048
#define	MAX_DEMOS		8
#define	MAX_DEMONAME	16

typedef enum
{
	ca_disconnected, 	// full screen console with no connection
	ca_connected		// valid netcon, talking to a server
} cactive_t;

// the client_static_t structure is persistant through an arbitrary number
// of server connections
struct client_static_t
{
	cactive_t	state;

	// personalization data sent to server
	char		mapstring[MAX_QPATH];
	char		spawnparms[MAX_MAPSTRING];	// to restart a level

	// demo loop control
	int			demonum;		// -1 = don't play demos
	char		demos[MAX_DEMOS][MAX_DEMONAME];		// when not playing
	int			numdemos;

	// demo recording info must be here, because record is started before
	// entering a map (and clearing client_state_t)
	BOOL	demorecording;
	BOOL	demoplayback;
	BOOL	timedemo;
	int			forcetrack;			// -1 = use normal cd track
	FILE		*demofile;
	int			td_lastframe;		// to meter out one message a frame
	int			td_startframe;		// host_framecount at start
	double		td_starttime;		// realtime at second frame of timedemo

	UINT		td_startPresent;	// vid.PresentCount at start

	// connection information
	int			signon;			// 0 to SIGNONS
	struct qsocket_s	*netcon;
	sizebuf_t	message;		// writing buffer to send to server

};

extern client_static_t	cls;

// the client_state_t structure is wiped completely at every
// server signon
struct client_state_t
{
	int			movemessages;	// since connecting to this server
	// throw out the first couple, so the player
	// doesn't accidentally do something the
	// first frame
	usercmd_t	cmd;			// last command sent to the server

	// information for local display
	int			stats[MAX_CL_STATS];	// health, etc
	int			items;			// inventory bit flags
	float	item_gettime[32];	// cl.time of aquiring item, for blinking
	float		faceanimtime;	// use anim frame if cl.time < this

	cshift_t	cshifts[NUM_CSHIFTS];	// color shifts for damage, powerups, etc

	// the client maintains its own idea of view angles, which are
	// sent to the server each frame.  The server sets punchangle when
	// the view is temporarliy offset, and an angle reset commands at the start
	// of each level and after teleporting.
	vec3_t		mviewangles[2];	// during demo playback viewangles is lerped
	// between these
	vec3_t		viewangles;

	vec3_t		mvelocity[2];	// update by server, used for lean+bob
	// (0 is newest)
	vec3_t		velocity;		// lerped between mvelocity[0] and [1]

	vec3_t		punchangle;		// temporary offset

	// pitch drifting vars
	float		idealpitch;
	float		pitchvel;
	BOOL	nodrift;
	float		driftmove;
	double		laststop;

	float		viewheight;
	float		crouch;			// local amount for smoothing stepups

	BOOL	paused;			// send over by server
	BOOL	onground;
	BOOL	inwater;

	int			intermission;	// don't change view angle, full screen, etc
	int			completed_time;	// latched at intermission start

	double		mtime[2];		// the timestamp of last two messages

	double		time;			// clients view of time, should be between
								// servertime and oldservertime to generate
								// a lerp point for other data

	int			protocol;

	// information that is static for the entire time connected to a server
	struct model_t		*model_precache[MAX_MODELS];
	struct sfx_s		*sound_precache[MAX_SOUNDS];

	char		levelname[40];	// for display on solo scoreboard
	int			viewentity;		// cl_entitites[cl.viewentity] = player
	int			maxclients;
	int			gametype;

	// refresh related state
	struct model_t	*worldmodel;	// cl_entitites[0].model
	int			num_entities;	// held in cl_entities array
	int			num_statics;	// held in cl_staticentities array
	entity_t	viewent;			// the gun model

	int			cdtrack, looptrack;	// cd audio

	// dynamic per-map data
	entity_t		*entities[MAX_EDICTS];

	// frag scoreboard
	scoreboard_t	*scores;		// [cl.maxclients]
};


// cvars
extern cvar_t *cl_upspeed;
extern cvar_t *cl_forwardspeed;
extern cvar_t *cl_backspeed;
extern cvar_t *cl_sidespeed;
extern cvar_t *cl_movespeedkey;
extern cvar_t *cl_yawspeed;
extern cvar_t *cl_pitchspeed;
extern cvar_t *cl_anglespeedkey;

extern cvar_t *cl_name;
extern cvar_t *cl_color;
extern cvar_t *cl_shownet;
extern cvar_t *cl_nolerp;
extern cvar_t *lookspring;
extern cvar_t *lookstrafe;
extern cvar_t *sensitivity;
extern cvar_t *m_pitch;
extern cvar_t *m_yaw;
extern cvar_t *m_forward;
extern cvar_t *m_side;

extern	client_state_t	cl;

// FIXME, allocate dynamically
extern	lightstyle_t	cl_lightstyle[MAX_LIGHTSTYLES];
extern	dlight_t		cl_dlights[MAX_DLIGHTS];

//=============================================================================

// cl_main
// keep dlight colours in the one place so that if i need to change them i only need to do it once
#define DL_COLOR_GREEN		308, 351, 109
#define DL_COLOR_PURPLE		399, 141, 228
#define DL_COLOR_BLUE		65, 232, 470
#define DL_COLOR_ORANGE		408, 242, 117
#define DL_COLOR_RED		460, 154, 154
#define DL_COLOR_YELLOW		384, 307, 77
#define DL_COLOR_WHITE		256, 256, 256

// dlight flags
#define DLF_KILL			(1 << 0)

dlight_t *CL_AllocDlight (int key, float radius, float die, float r, float g, float b);
void	CL_DecayLights (void);

void CL_Init (void);

void CL_EstablishConnection (char *host);
void CL_Signon1 (void);
void CL_Signon2 (void);
void CL_Signon3 (void);
void CL_Signon4 (void);

void CL_Disconnect (void);
void CL_Disconnect_f (void);
void CL_NextDemo (void);

extern	int				cl_numvisedicts;
extern	entity_t		*cl_visedicts[MAX_EDICTS];

// cl_input
typedef struct
{
	int		down[2];		// key nums holding it down
	int		state;			// low bit is down state
} kbutton_t;

extern	kbutton_t	in_mlook, in_klook;
extern 	kbutton_t 	in_strafe;
extern 	kbutton_t 	in_speed;

void CL_InitInput (void);
void CL_SendCmd (double frametime);
void CL_SendMove (usercmd_t *cmd);

void CL_ParseTEnt (void);
void CL_UpdateTEnts (void);

void CL_ClearState (void);
void CL_ReadFromServer (double frametime);

void CL_BaseMove (double frametime, usercmd_t *cmd);
void CL_BoundViewAngle (int angle, float amin, float amax);

float CL_KeyState (kbutton_t *key);
const char *Key_KeynumToString (int keynum);

// cl_demo.c
void CL_StopPlayback (void);
int CL_GetMessage (void);

void CL_Stop_f (void);
void CL_Record_f (void);
void CL_PlayDemo_f (void);
void CL_TimeDemo_f (void);

// cl_parse.c
void CL_ParseServerMessage (void);
void CL_NewTranslation (int slot);

// view
void V_RenderView (double frametime);
void V_UpdatePalette (void);
void V_Register (void);
void V_ParseDamage (void);
void V_CalcBlend (void);


// cl_tent
void CL_NewTentsForNewMap (void);
void CL_SignonReply (void);

void CL_InitEffects (void);
int CL_TimedRandom (float offset, float rate);

void CL_RelinkEntities (void);
void CL_UpdateTEnts (void);

void CL_RocketTrail (entity_t *ent, int type);
void CL_ClearRocketTrail (entity_t *ent);

#define MoveLinkToFreeList(a,b,c) \
	(a) = (b)->next; \
	(b)->next = (c); \
	(c) = (b);

#define GetLinkFromFreeList(activelist, thislink, freelist) \
	(thislink) = (freelist); \
	(freelist) = (thislink)->next; \
	(thislink)->next = (activelist); \
	(activelist) = (thislink);

void CL_TraceLine (vec3_t start, vec3_t end, vec3_t impact);
BOOL CL_RecursiveHullCheck (struct hull_t *hull, int num, float p1f, float p2f, vec3_t p1, vec3_t p2, struct trace_t *trace);
int CL_HullPointContents (struct hull_t *hull, int num, vec3_t p);

